<template>
  <div class="lw-list">
    <slot></slot>
    <div class="loading" v-show="value">加载中</div>
    <div class="finish" v-if="finished">{{ finishedText }}</div>
  </div>
</template>

<script>
import debounce from "@/utils/debounce.js";

export default {
  name: "Lw-list",
  props: ["finished-text", "finished", "loading", "value"],
  methods: {
    emitLoad() {
      this.$emit("load");
    },
  },
  async mounted() {
    this.emitLoad();
    window.addEventListener(
      "scroll",
      debounce(
        () => {
          let scrollHeight = document.documentElement.scrollHeight;
          let scrollTop = document.documentElement.scrollTop;
          let innerHeight = window.innerHeight;
          console.log(scrollTop);
          if (scrollHeight - scrollTop - innerHeight < 50) {
            this.emitLoad();
          }
        },
        250,
        false
      )
    );
  },
};
</script>

<style lang="less" scoped>
.finish,
.loading {
  text-align: center;
  color: #bbb;
  height: 1rem;
  line-height: 1rem;
  font-size: 0.4rem;
  background: #efefef;
}
</style>
